using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Common;
using com.paypal.sdk.profiles;
using com.paypal.sdk.services;
using com.paypal.sdk.util;

namespace Sedna.Payment.PayPal
{
    public class ExpressCheckoutAction : PayPalAction
    {
        private string currencyCode = "USD";

        private string amount = "0";

        private string returnURL = string.Empty;
        private string cancelURL = null;

        private string transactionTokenUnbindPath;

        #region Properties
        public string CurrencyCode
        {
            get { return currencyCode; }
            set { currencyCode = value; }
        }

        public string Amount
        {
            get { return amount; }
            set { amount = value; }
        }

        public string ReturnUrl
        {
            get { return returnURL; }
            set { returnURL = value; }
        }

        public string TransactionTokenUnbindPath
        {
            get { return transactionTokenUnbindPath; }
            set { transactionTokenUnbindPath = value; }
        }
        #endregion

        
        public override IActionResult Execute(IExecutionContext executionContext)
        {
            IAPIProfile profile = ProfileFactory.createSignatureAPIProfile();
            profile.APIUsername = Api_Username;
            profile.APIPassword = Api_Password;
            profile.APISignature = Api_Signature;
            profile.Subject = string.Empty;
            profile.Environment = Environment;
            returnURL += "?amount=" + amount + "&paymentType=Sale";

            NVPCallerServices caller = new NVPCallerServices();
            caller.APIProfile = profile;

            NVPCodec encoder = new NVPCodec();
            encoder["METHOD"] = "SetExpressCheckout";
            encoder["RETURNURL"] = returnURL;
            if (cancelURL == null)
            {
                encoder["CANCELURL"] = HttpContext.Current.Request.Url.ToString();
            }
            else
            {
                encoder["CANCELURL"] = cancelURL;
            }
            encoder["AMT"] = amount;
            encoder["PAYMENTACTION"] = "Sale";
            encoder["CURRENCYCODE"] = currencyCode;

            string requestForNvp = encoder.Encode();
            string nvpResponse = caller.Call(requestForNvp);

            NVPCodec decoder = new NVPCodec();
            decoder.Decode(nvpResponse);

            string strAck = decoder["ACK"];
            if (strAck != null && (strAck == "Success" || strAck == "SuccessWithWarning"))
            {
                string transactionToken = decoder["TOKEN"];
                executionContext.SetPropertyValue(transactionTokenUnbindPath, transactionToken);
                executionContext.CallContext.Add("PayPalTransactionToken", transactionToken);
                executionContext.CallContext.Add("PayPalEnvironment", Environment);
                return CreateResult(successStatus);
            } 
            else
            {
                return CreateErrorResult(executionContext, nvpResponse);
            }

        }
    }
}
